package com.hsav.kcxb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hsav.kcxb.R;
import com.hsav.kcxb.bean.GiftItemBean;
import com.hsav.kcxb.utils.ImgLoadUtil;
import com.zhy.autolayout.utils.AutoUtils;

/**
 * Created by zhao on 2017/2/5.
 */

public class GiftListAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private final Context mContext;
    private final LayoutInflater mInflater;
    private int selectedPosition;
    private View lastSelected;

    public GiftListAdapter(Context context){
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = mInflater.inflate(R.layout.item_gift_view, parent, false);
        rootView.setOnClickListener(this);
        return new ViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder)holder;
        viewHolder.itemView.setTag(position);
        // TODO: 2017/6/22
    }

    @Override
    public int getItemCount() {
        return 10;
    }

    @Override
    public void onClick(View v) {
        selectedPosition = (int)v.getTag();
        Toast.makeText(mContext, String.format("Position: %s", selectedPosition), Toast.LENGTH_SHORT).show();
        doSelect(v);
    }

    private void doSelect(View selectedView) {
        if (selectedView == lastSelected) return;
        if (lastSelected != null)
        {
            lastSelected.setBackgroundResource(0);
        }
        selectedView.setBackgroundResource(R.drawable.border);
        lastSelected = selectedView;
    }

    private class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView imgGift;
        public TextView textName;
        public TextView textPrice;

        public ViewHolder(View itemView) {
            super(itemView);
            AutoUtils.autoSize(itemView);
            imgGift = (ImageView) itemView.findViewById(R.id.img_gift);
            textName = (TextView)itemView.findViewById(R.id.text_name);
            textPrice = (TextView)itemView.findViewById(R.id.text_price);
        }
    }
}
